package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Product;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.IProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品单位 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-03-12
 */
@Tag(name = "商品单位")
@RestController
@RequestMapping("/productUnit")
public class ProductUnitController extends BaseController {


    @Autowired
    private IProductUnitService productUnitService;
    @Operation(summary = "获取商品单位列表")
    @GetMapping("/list")
    public TableDataInfo list(@Parameter(name = "name", description = "商品单位名称") String name) {

        // 开启分页查询
        startPage();
        // 查询品牌列表 根据名称模糊查询 名称非空判断，防止参数为null 拼就进sql语句
        List<ProductUnit> list = productUnitService.list(new LambdaQueryWrapper<ProductUnit>().like(StringUtils.hasText(name),ProductUnit::getName, name));
        return getDataTable(list);
    }

    // 2. 获取单位详情信息
    @Operation(summary = "根据id获取单位详情信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@Parameter(name = "id", description = "商品单位id") @PathVariable Long id) {

        return success(productUnitService.getById(id));
    }
    // 3. 新增商品单位
    @Operation(summary = "新增商品单位")
    @PostMapping
    public AjaxResult add(@Parameter(name = "productUnit", description = "商品单位信息") @RequestBody ProductUnit productUnit) {

        // 设置操作人
        productUnit.setCreateBy(SecurityUtils.getUsername());
        return toAjax(productUnitService.save(productUnit));
    }

    // 4. 修改商品单位
    @Operation(summary = "修改商品单位")
    @PutMapping
    public AjaxResult edit(@Parameter(name = "productUnit", description = "商品单位信息") @RequestBody ProductUnit productUnit) {

        // 设置操作人
        productUnit.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(productUnitService.updateById(productUnit));
    }
    // 5. 删除商品单位
    @Operation(summary = "删除商品单位")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@Parameter(name = "ids", description = "商品单位id列表") @PathVariable List<Integer> ids) {

        return toAjax(productUnitService.removeByIds(ids));
    }
}
